# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  def is_on?(on, index)
    on.to_i == index.to_i ? "class='select'" : ""
  end
  
  def show_category_for(item)
    [item.try(:cat1_name), item.try(:cat2_name)].reject{|x| x.blank? }.join(' > ')
  end
  
  def assert_current_user?
    return false unless logged_in?
    current_user.id == params[:id].to_i
  end
  
  def truncate_u(text, length = 30, truncate_string = "...")
    return "" if text.nil?
    l=0
    char_array=text.unpack("U*")
    char_array.each_with_index do |c,i|
      l = l+ (c < 127 ? 0.5 : 1)
      if l >= length
        return char_array[0..i].pack("U*")+( i < char_array.length-1 ? truncate_string : "")
      end
    end
    return text
  end

end
